import redis
from wxpy import Bot
import pymongo
from baseHandler import init_logger

logger = init_logger('cmcc', '/app/log/cs_cmcc/cmcc_failed.log')
conn = redis.Redis(db=0)
bot = Bot(console_qr=2, cache_path="botoo.pkl")
client = pymongo.MongoClient('mongodb://cs_user:cmcc5188@localhost:12018/cs_recharge')
db = client.cs_recharge
# grp = bot.friends().search('8583')[0]
groups = bot.groups()
group = None
for i in groups:
    if i.name == '127星恒太':
        group = i
if not group:
    logger.info("找不到群")


while True:
    res = conn.brpop("cmcc_failed")
    logger.info("%s|充值失败", res)
    ordernumber = res[1].decode()
    obj = db.rechange.find_one({"ordernumber": ordernumber})
    if obj['merchantid'] == 'M2018081416281534235296':
        group.send("%s充值失败|%s|%s" % (ordernumber, obj.get("prov"), obj.get("tel_number")))
        #print("%s充值失败|%s|%s" % (ordernumber, obj.get("prov"), obj.get("tel_number")))


